package nuc.ss.wl.dao.impl;

import nuc.ss.wl.dao.ClassDao;
import nuc.ss.wl.entity.Class;
import nuc.ss.wl.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ClassDaoImpl implements ClassDao {

    @Override
    public int save(Class clazz) {
        String sql = "insert into class values(?,?,?,?)";
        Object[] params = {
                clazz.getClassno(),
                clazz.getTno(),
                clazz.getClassname(),
                clazz.getClassgrade()
        };
        return DBUtil.executeUpdate(sql,params);
    }

    @Override
    public List<Class> findAll() {
        //声明jdbc变量
        Connection conn = null;
        PreparedStatement ps=null;
        ResultSet rs = null;

        //声明变量
        List<Class> list = null;
        try {
            //创建连接
            conn = DBUtil.getConnection();
            String sql = "select * from class";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            list = new ArrayList<Class>();
            while(rs.next()){
                int classno = rs.getInt(1);
                int tno = rs.getInt(2);
                String classname = rs.getString(3);
                int classgrade = rs.getInt(4);
                Class clazz = new Class();
                clazz.setClassno(classno);
                clazz.setTno(tno);
                clazz.setClassname(classname);
                clazz.setClassgrade(classgrade);

                list.add(clazz);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.closeAll(rs,ps,conn);
        }
        return list;
    }

    @Override
    public int delete(int classno) {
        String sql = "delete from class where classno = ?";
        Object[] params = {classno};
        return DBUtil.executeUpdate(sql,params);
    }

    @Override
    public Class findById(int classno) {
        //声明jdbc变量
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        //声明变量
        Class clazz = null;
        try{
            conn = DBUtil.getConnection();
            String sql = "select * from class where classno = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,classno);
            rs = ps.executeQuery();
            while(rs.next()){
                int tno = rs.getInt(2);
                String classname = rs.getString(3);
                int classgrade = rs.getInt(4);
                clazz = new Class();
                clazz.setClassno(classno);
                clazz.setTno(tno);
                clazz.setClassname(classname);
                clazz.setClassgrade(classgrade);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.closeAll(rs,ps,conn);
        }
        return clazz;
    }

    @Override
    public int update(Class clazz) {
        String sql = "update class set tno = ?,classname=?,classgrade=? where classno = ?";
        Object[] params = {
                clazz.getTno(),
                clazz.getClassname(),
                clazz.getClassgrade(),
                clazz.getClassno()
        };
        return DBUtil.executeUpdate(sql,params);
    }
}
